/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * Description
 *
 * User: jinchen
 * Date: Jun 2, 2003
 * Time: 10:19:13 AM
 */
package edu.psu.geovista.app.plot.axis;

import edu.psu.geovista.app.pcp.toolkits.Validator;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;

public class AxisYFooter extends AxisYHeader{
    public static final Dimension FULLSIZE=new Dimension(15,15);
    public static final Dimension COMPACTSIZE=new Dimension(10,10);
    protected Dimension cSize; //colorBar size
    //protected Color[]  colors;
    protected Rectangle outLine;
    private ColorBar cbar;
    private boolean filledShape;// if true, paint a filled shape. Otherwise, just paint boundary of the shape

    public AxisYFooter(YAxis axis) {
        super(axis);
        this.setSize(FULLSIZE);//new Dimension(15,15));
       //setup colorbar
        cSize=new Dimension(size.width*2 /3,size.height/3);



        //this.setColors(colors);
    }
    public void paintComponent(Graphics g){
        Graphics2D g2d=( Graphics2D)g;

        Point oLoc=this.getDrawingOLocation() ;

        //outline
        //int x=oLoc.x-Math.round(size.width /2);
        //int y=oLoc.y+YAxis.FOOTER_PAD ;
        Stroke olds=g2d.getStroke() ;
        BasicStroke bs=new BasicStroke(2);
        g2d.setStroke(bs);
        //1. draw outLine
        Rectangle r=this.getOutLine() ;
        Point loc=r.getLocation() ;
        int h=(int) r.getHeight() ;
        int w=(int)r.getWidth() ;
        if(this.isFilledShape() ){
            g2d.fill(r);
        }
        else{
            g2d.draw(r);
        }
       //g2d.drawRect(x,y,size.width,size.height );
        g2d.setStroke(olds);
        //this.outLine.setBounds(x,y,size.width,size.height) ;
        //2. draw indicator of YAxis's display type
        if (axis.getDisplayType() ==YAxis.DTYPE_Qty ){
            g2d.drawLine(loc.x,loc.y,loc.x+w,loc.y+h);
            g2d.drawLine(loc.x+w,loc.y,loc.x,loc.y+h);
        }
        else if(axis.getDisplayType() ==YAxis.DTYPE_Ord ){
            int midy=(loc.y+h/2) ;
            g2d.drawLine(loc.x,midy,loc.x+w,midy);
        }
        //title
        //3. Category
        if (axis.getCategoryList()!=null){
            int x, y;
            x=oLoc.x-Math.round(size.width /3);
            y=oLoc.y+Math.round(size.height *2/3);
            String title=axis.getCategoryList().getCategoryMethodName() ;
            if (Validator.isValidString(title)){

                Font old=g2d.getFont() ;
                g2d.setFont(this.font );
                //BasicGraphicsUtils.drawStringUnderlineCharAt(g, title, 0,x, y);
                g2d.drawString(title.toUpperCase() ,x,y);
                g2d.setFont(old);
            }


            //color bar
            ArrayList ctgList=Collections.list(this.axis.getCategoryList().getCtgList().elements() );
            cbar=new ColorBar(null,cSize, ctgList,false);
            if (cbar!=null){
                y=y+5;
                x= (int)((oLoc.getX()-cSize.getWidth()/2));
                cbar.setPosition(new Point(x,y));
                cbar.paintComponent(g2d);
            }
        }

    }

    /**
    *  see if the given point is within the footer's outline
    */
    public boolean contain(Point p) {
        return this.outLine.contains(p);

    }
    /**
    *  see if the given point is within the footer's outline
    */
    public boolean contain(int x, int y) {
        Rectangle outLine = this.outLine;
        //System.out.println("outline:"+outLine);
        //System.out.println("point:"+x+","+y);

        boolean contained = outLine.contains(x,y);
        //System.out.println("contain:"+contained);
        return contained;

    }
    /**
     *  return  location of drawing O point which all drawing is relative to.
     */
    private Point getDrawingOLocation(){
        return new Point(this.getAxisLocation().x,this.getAxisLocation().y+axis.getActiveSize() );
    }
    /**
     *
     */
    private void setOutLine(Rectangle rect){
           Point oLoc=this.getDrawingOLocation() ;//footer drawing point
           int x=oLoc.x-Math.round(size.width /2);
           int y=oLoc.y+YAxis.FOOTER_PAD ;

           rect.setBounds(x,y,size.width ,size.height ) ;

    }
    public void setFont(Font font) {
    }
    public void setSize(Dimension size) {
        this.size = size;
        this.font=new Font(this.style,Font.BOLD,size.height*2/5 ); //font size depend on size
        this.getOutLine().setSize(this.size);
        this.repaint() ;
    }


    public Rectangle getOutLine() {
        if (this.outLine ==null)  {
            this.outLine =new Rectangle();
        }

        this.setOutLine(outLine);
        return outLine;
    }

    public boolean isFilledShape() {
        return filledShape;
    }

    public void setFilledShape(boolean filledShape) {
        this.filledShape = filledShape;
    }
     public void setPaintmode(int paintmode) {
        super.setPaintmode(paintmode);
        if(this.getPaintmode() ==YAxis.PAINT_MODE_SIMPLE){
              this.setSize(COMPACTSIZE);
        }
        else{
             this.setSize(FULLSIZE);
        }
    }
}
